/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RichTreeExample.controller;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


import org.richfaces.component.html.HtmlTree;
import org.richfaces.event.NodeSelectedEvent;
import org.richfaces.model.TreeNode;
import org.richfaces.model.TreeNodeImpl;

public class SimpleTreeBean {

    private TreeNodeImpl<String> stationRoot = new TreeNodeImpl<String>();
    private TreeNodeImpl<String> stationRoot1 = new TreeNodeImpl<String>();
    private TreeNodeImpl<String> stationNodes = new TreeNodeImpl<String>();
    private String[] kickRadioFeed = {"Hall & Oates - Kiss On My List",
        "David Bowie - Let's Dance", "Lyn Collins - Think (About It)",
        "Kim Carnes - Bette Davis Eyes",
        "KC & the Sunshine Band - Give It Up"};
    private List<String> selectedNodeChildren = new ArrayList<String>();
    private String nodeTitle;
    //private static final String DATA_PATH = "/src/java/arbol/cantantes.properties";
    static int pos = 0;

    public SimpleTreeBean() {
        /*1.1.1 = titulo
        String codigo=lista.get(pos).getId_item();  //01M02C01 T1M00C00 tomo 1
        String titulo=lista.get(pos).getNombre();  //Capitulo 1

        String key=codigo.substring(num,num+1);



        while (!end) {
        //String key = path != null ? path + '.' + counter : String.valueOf(counter);

        String value = titulo;
        if (value != null) {
        TreeNodeImpl nodeImpl = new TreeNodeImpl();
        nodeImpl.setData(value);
        node.addChild(new Integer(counter), nodeImpl);
        pos++;
        addNodes(key, nodeImpl, lista,num+2);
        counter++;
        } else {
        end = true;
        }
        }*/
    }

    public void loadTree() {

        stationRoot.setData("KickRadio");
        stationRoot1.setData("Segunda carpeta");

        stationNodes.addChild(0, stationRoot);
        stationNodes.addChild(1, stationRoot1);


        for (int i = 0; i < kickRadioFeed.length; i++) {

            TreeNodeImpl<String> child = new TreeNodeImpl<String>();

            child.setData(kickRadioFeed[i]);

            stationRoot.addChild(i, child);
            stationRoot1.addChild(i, child);

        }

    }

    public TreeNodeImpl<String> getStationNodes() {
        loadTree();
        return stationNodes;
    }

    public void setStationNodes(TreeNodeImpl<String> stationNodes) {
        this.stationNodes = stationNodes;
    }

    public TreeNodeImpl<String> getStationRoot() {



        return stationRoot;
    }

    public void setStationRoot(TreeNodeImpl<String> stationRoot) {
        this.stationRoot = stationRoot;
    }

    /* private void loadTree() {

    try {

    ArrayList<BeanItemprueba> lista = DaoArbol.tablaarbol();
    rootNode = new TreeNodeImpl();
    addNodes(null, rootNode, lista,0);

    } catch (Exception e) {
    System.out.println(e.getMessage());
    }

    }
     */
    public void processSelection(NodeSelectedEvent event) {
        HtmlTree tree = (HtmlTree) event.getComponent();
        nodeTitle = (String) tree.getRowData();
        selectedNodeChildren.clear();
        TreeNode currentNode = tree.getModelTreeNode(tree.getRowKey());
        if (currentNode.isLeaf()) {
            selectedNodeChildren.add((String) currentNode.getData());
        } else {
            Iterator<Map.Entry<Object, TreeNode>> it = currentNode.getChildren();
            while (it != null && it.hasNext()) {
                Map.Entry<Object, TreeNode> entry = it.next();
                selectedNodeChildren.add(entry.getValue().getData().toString());
            }
        }
    }

    public String getNodeTitle() {
        return nodeTitle;
    }

    public void setNodeTitle(String nodeTitle) {
        this.nodeTitle = nodeTitle;
    }
}
